package com.example.demo.common;

import lombok.Data;

/**
 * 通用响应结果类
 * @author Logan.Li
 */
@Data
public class Result<T> {
    
    /**
     * 状态码
     */
    private Integer code;
    
    /**
     * 消息
     */
    private String msg;
    
    /**
     * 错误信息
     */
    private String errors;
    
    /**
     * 数据
     */
    private T data;
    
    /**
     * 成功结果
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(20000);
        result.setMsg("成功");
        result.setErrors("");
        result.setData(data);
        return result;
    }
    
    /**
     * 失败结果
     */
    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(50000);
        result.setMsg("失败");
        result.setErrors(message);
        return result;
    }
    
    /**
     * 自定义状态码的结果
     */
    public static <T> Result<T> custom(Integer code, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(message);
        result.setData(data);
        return result;
    }
}
